<template>
  <div>
        <h3 class="titleH3">基本信息</h3>
        <FormPanel
            :list="FormPanelList"
            :cols="3"
            :labelWidth="120"
            :isSubmitBtn='false'
            :formType="type"
            ref="FormPanel1"
            :collapse="false"
        ></FormPanel>
        <div v-show="isShow">
          <h3 class="titleH3">管理员账号信息</h3>
          <FormPanel
              :list="FormPanelList0"
              :cols="3"
              :labelWidth="120"
              :isSubmitBtn='false'
              :formType="type"
              ref="FormPanel"
              :collapse="false"
          ></FormPanel>
        </div>
      <div style="text-align: center;margin: 20px 0 20px 0;">
          <el-button size="small" type=""  @click="closeHandler">返回</el-button>
          <el-button size="small" v-show="type == 'show'?false:true" type="primary"  @click="saveHandle">保存</el-button>
      </div>

      <!-- <BaseDialog 
      :visible.sync="visible" 
      title="新增授权"
      width="30%"
      destroyOnClose>
        <FormPanel
          :list="FormPanelList2"
          :cols="1"
          :labelWidth="120"
          :isSubmitBtn='false'
          formType="edit"
          :collapse="false"
        ></FormPanel>
        <div style="text-align: center;margin: 20px 0 20px 0;">
          <el-button size="small" type=""  @click="cancal">返回</el-button>
          <el-button size="small" v-show="type == 'show'?false:true" type="primary"  @click="addSaveHandle">保存</el-button>
      </div>
    </BaseDialog> -->
  </div>
</template>

<script>
import { mapState } from 'vuex';
import { dictionary } from '@/utils/dictMixin';
import Cookies from 'js-cookie';
import {addCompany,getCompanyId} from '@/api/common/org/gms/dealerCompany';
import * as types from '@/api/common/org/gms/dealerCompany/types';
import { IDNumber, banWord,phone,testNumLetter8,testPW } from '@/utils/validate'
export default {
  name: 'modal',
  mixins: [dictionary],
  components: {
  },
  props:['rowData','type','listAllData'],
  data() {
    this.BaseTable = null;
    return {
        FormPanelList0:this.createFormPanelList0(),
        FormPanelList: this.createFormPanelList(),
        // FormPanelList2:this.createFormPanelList2(),
        dataSource:[{ billType: '1', brandId: '1' }],
        isShow: false,
        visible:false,
        rowDataTable:'',
        province:[],
        dialogTitle:'',
        isHidden:70401002,
        smsType:Cookies.get('smsType'),
        wechatType:Cookies.get('wechatType'),
    };
  },
  methods: {
      createFormPanelList() {
      return [
        {
          type: 'INPUT',
          label: '公司代码',
          fieldName: 'companyCode',
          placeholder: '',
          style: { width: '240px' },
          disabled:false,
          maxlength:8,
          change:val=>{
            console.log(val,"val")
            if(this.type == 'add'){
              this.FormPanelList0.find(item=>item.fieldName == "userCode").initialValue=val+"ADMIN"
            }
          },
          rules: [{ required: true, validator:testNumLetter8, trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '公司名称',
          fieldName: 'companyNameCn',
          placeholder: '',
          // disabled:false,
          style: { width: '240px' },
          maxlength:32,
          rules: [{ required: true, message: '请输入公司名称', trigger: 'change' },
          { min: 1, max: 32, message: '长度在 1 到 32 个字符', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '公司简称',
          fieldName: 'companyShortNameCn',
          placeholder: '',
          style: { width: '240px' },
          maxlength:16,
          rules: [{ required: true, message: '请输入公司简称', trigger: 'change' },
          { min: 1, max: 16, message: '长度在 1 到 16 个字符', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '省',
          fieldName: 'provinceId',
          placeholder: '',
          style: { width: '240px' },
          itemList: [],
          change:val=>{
            let city=[]
            if(val == 3662){
              this.FormPanelList[4].itemList=[{text:"台湾",value:3662}]
              this.FormPanelList[5].itemList=[{text:"台湾",value:3662}]
            }else if(val == 3663){
              this.FormPanelList[4].itemList=[{text:"香港",value:3663}]
              this.FormPanelList[5].itemList=[{text:"香港",value:3663}]
            }else if(val == 3664){
              this.FormPanelList[4].itemList=[{text:"澳门",value:3664}]
              this.FormPanelList[5].itemList=[{text:"澳门",value:3664}]
            }
            this.FormPanelList[4].initialValue=''
            this.FormPanelList[5].initialValue=''
            this.listAllData.forEach(x=>{
              // 筛选省
              if(x.parentRegionId == val){
                city.push({text:x.regionName,value:x.regionId})
                this.FormPanelList[4].itemList=_.uniqBy(city,'value')
              }
            })
          },
          rules: [{ required: true, message: '请选择', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '市',
          fieldName: 'cityId',
          placeholder: '',
          style: { width: '240px' },
          itemList: [],
          change:val=>{
            let county=[]
            this.FormPanelList[5].initialValue=''
            this.listAllData.forEach(x=>{
              // 筛选省
              if(x.parentRegionId == val){
                county.push({text:x.regionName,value:x.regionId})
                this.FormPanelList[5].itemList=_.uniqBy(county,'value')
              }
            })
          },
          rules: [{ required: true, message: '请选择', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '区/县',
          fieldName: 'county',
          placeholder: '',
          style: { width: '240px' },
          itemList: [],
        },
        {
          type: 'INPUT',
          label: '地址',
          fieldName: 'addressZh',
          placeholder: '',
          style: { width: '240px' },
          maxlength:32,
          rules: [{ required: true, message: '请输入地址', trigger: 'change' },
          { min: 1, max: 32, message: '长度在 1 到 32 个字符', trigger: 'change' }]
        },
         {
          type: 'INPUT',
          label: '联系电话',
          fieldName: 'phone',
          placeholder: '',
          style: { width: '240px' },
          // maxlength:11,
          rules: [{ required: true, message: '请输入联系电话', trigger: 'change' },
          { trigger: 'blur', validator: banWord }]
        },
        {
          type: 'INPUT',
          label: '传真',
          fieldName: 'fax',
          placeholder: '',
          style: { width: '240px' },
          maxlength:32,
          // rules: [{ required: true, message: '请选择', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: 'Email',
          fieldName: 'eMail',
          placeholder: '',
          style: { width: '240px' },
          maxlength:32,
          // rules: [{ required: true, message: '请选择', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '邮编',
          fieldName: 'zipCode',
          placeholder: '',
          style: { width: '240px' },
          maxlength:6,
          // rules: [{ required: true, message: '请选择', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '公司状态',
          fieldName: 'validStatus',
          placeholder: '',
          style: { width: '240px' },
          itemList: this.createDictList('1001'),
          rules: [{ required: true, message: '请选择市', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '公司主页',
          fieldName: 'companyHomepage',
          placeholder: '',
          style: { width: '240px' },
          maxlength:32,
          // rules: [{ required: true, message: '请选择', trigger: 'change' }]
        },
        {
          type: 'DATE',
          label: '开业时间',
          fieldName: 'openDate',
          placeholder: '',
          dateType:'exactdate',
          style: { width: '240px' },
          // rules: [{ required: true, message: '请选择', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '备注',
          fieldName: 'remark',
          placeholder: '',
          style: { width: '240px' },
          maxlength:64,
          itemList: [],
        },
        
      ];
    },
    createFormPanelList0() {
        return [
          {
          type: 'INPUT',
          label: '管理员账号',
          fieldName: 'userCode',
          placeholder: '',
          disabled:false,
          initialValue:'',
          style: { width: '240px' },
          rules: [{ required: true, message: '请输入管理员账号', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '管理员密码',
          fieldName: 'password',
          placeholder: '',
          disabled:false,
          initialValue:'Aa123456@',
          style: { width: '240px' },
          rules: [{ required: true, validator:testPW, trigger: 'change' }]
          // rules: [{ required: true, message: '请输入管理员密码', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '管理员手机',
          fieldName: 'mobilePhone',
          hidden:Cookies.get('smsType') == 10041001?false:true,
          placeholder: '',
          maxlength:11,
          style: { width: '240px' },
        },
        ];
      },
    
    init(){
        
        if(this.type == 'edit'){
            this.FormPanelList[0].disabled=true
            this.FormPanelList0[0].disabled=true
            this.FormPanelList0[1].disabled=true
        }else{
            this.FormPanelList[0].disabled=false
            this.FormPanelList[1].disabled=false
        }
        // 查看管理员信息
        if(this.type != 'add'){
          
          getCompanyId(this.rowData.companyId).then(res=>{
            console.log(res,"Res")
           
            this.FormPanelList0[0].initialValue =  res.data.userCode
            this.FormPanelList0[1].initialValue =  res.data.password
            this.FormPanelList0[2].initialValue =  res.data.mobilePhone
          })
        }
        this.FormPanelList.forEach(ele=>{
            ele.initialValue=this.rowData[ele.fieldName]
        })

        if(this.rowData.companyCode){
          this.FormPanelList0.find(item=>item.fieldName == "userCode").initialValue=this.rowData.companyCode+"ADMIN"
        }

        //初始化省市区赋值
        let initCity=[]
        let initCounty=[]
        this.listAllData.forEach(x=>{

            // 筛选省
            if(x.parentRegionId == null){
              this.province.push({text:x.regionName,value:x.regionId})
            }

            // 筛选市
            if(x.parentRegionId == this.FormPanelList[3].initialValue){
              initCity.push({text:x.regionName,value:x.regionId})
            }

            // 筛选区
            if(x.parentRegionId == this.FormPanelList[4].initialValue){
              initCounty.push({text:x.regionName,value:`${x.regionId}`})
            }
        })
        this.FormPanelList[3].itemList=_.uniqBy(this.province,'value')
        this.FormPanelList[4].itemList=_.uniqBy(initCity,'value')
        this.FormPanelList[5].itemList=_.uniqBy(initCounty,'value')
        if(this.type == 'add'){
          this.isShow=true
          this.FormPanelList[4].itemList=[]
          this.FormPanelList[5].itemList=[]
        }

        if(this.type == 'show'){
          this.FormPanelList0.forEach(ele=>{
              ele.rules=[]
          })
          this.FormPanelList.forEach(ele=>{
              ele.rules=[]
          })
        }
    },
    getCookie(){
        let allCookie=document.cookie.split(';')
        allCookie.forEach(x=>{
          if(x.indexOf('dataType') > -1 ){
            this.dataType=x.split('=')[1]
            }
        })
        //判断是否为车厂10461003
        console.log(this.dataType,"dataType")
        if(this.dataType == 10461003){
          console.log(this.dataType,"车厂")
        }
      },
    // 新增
    addTable(){
        this.visible=true
    },
    // 编辑
    editFun(row) {
      console.log(row,"编辑")
      this.visible=true
      this.rowDataTable=row
    },
    // 删除
    deletFun(row) {
      this.$confirm('确定删除吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          const res = await deleteRole(row.roleId)
          if(res.errMsg) {
            this.$notify.error({title: '提示信息',message:res.errMsg||'删除失败!'});
          }else {
            this.$notify.success({title: '提示信息',message:'删除成功!'});
            // 重新加载
            this.fetchParams = { ...this.fetchParams };
          }
        }).catch(() => {
          // this.$notify.info({title: '提示信息',message:'已取消删除'});
        });
    },
   
    // 保存
    saveHandle(){
      this.$refs.FormPanel.GET_FORM_DATA()
      let saveData2 =this.$refs.FormPanel1.SUBMIT_FORM()

      /* this.FormPanelList0[0].initialValue && this.FormPanelList0[1].initialValue && */
      if( this.FormPanelList[0].initialValue && this.FormPanelList[1].initialValue && this.FormPanelList[2].initialValue && this.FormPanelList[3].initialValue && this.FormPanelList[4].initialValue && this.FormPanelList[6].initialValue&& this.FormPanelList[7].initialValue&& this.FormPanelList[11].initialValue){
       if(this.type == 'edit'){
         let params={
          userCode:this.FormPanelList0[0].initialValue,
          password:this.FormPanelList0[1].initialValue,
          mobilePhone:this.FormPanelList0[2].initialValue?this.FormPanelList0[2].initialValue:"",
          ...saveData2,
          companyId:this.rowData.companyId
        }

        console.log(params,"新增")
        // if(this.type == 'add'){
            addCompany(params).then(res=>{
              if(res.resultCode == 200){
                this.$notify.success({title: '提示信息',message:"保存成功！"})
                this.closeHandler('refresh')
              }else{
                // this.$notify.error({title: '提示信息',message:res.errMsg})
              }
            })
        // }else{
        //   console.log(this.type,"edit")
        // }
       }else{
         let params2={
          userCode:this.FormPanelList0[0].initialValue,
          password:this.FormPanelList0[1].initialValue,
          mobilePhone:this.FormPanelList0[2].initialValue?this.FormPanelList0[2].initialValue:"",
          ...saveData2,
        }

        console.log(params2,"新增")
        // if(this.type == 'add'){
            addCompany(params2).then(res=>{
              if(res.resultCode == 200){
                this.$notify.success({title: '提示信息',message:"保存成功！"})
                this.closeHandler('refresh')
              }else{
                // this.$notify.error({title: '提示信息',message:res.errMsg})
              }
            })
        // }else{
        //   console.log(this.type,"edit")
        // }
       }
        // this.FormPanelList.forEach(ele=>{
        //   Object.keys(params).forEach(x=>{
        //     if(x == ele.fieldName){
        //         params[x] =ele.initialValue
        //     }
        //   })
        // })
       
        
        
      }else{
        this.$notify.error({title: '提示信息',message:"填写必填项"})
      }
    },
    //新增保存
    // addSaveHandle(){
    //   console.log(this.FormPanelList2)
    // },
    // 新增返回
    cancal(){
      this.visible=false
    },
    closeHandler(type) {
        this.$emit('close',type)
    }
  },
  mounted() {
    this.getCookie()
    this.init()
  }
};
</script>

<style lang="less" scoped>
.titleH3{
    height: 30px;
    line-height: 30px;
    margin-bottom: 5px;
    background: #eee;
    text-indent: 5px;
}
</style>
